package org.particles.domain.world;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.particles.domain.LinearFunction;
import org.particles.domain.field.AbstractField;
import org.particles.domain.field.AttractionForce;
import org.particles.domain.field.Field;
import org.particles.domain.field.RejectionForce;
import org.particles.domain.field.StringForce;

public class SimpleFieldRepository implements FieldRepository {
	private final Map<Long, Field> markerToFieldMap = new HashMap<Long, Field>();
	
	public SimpleFieldRepository() {
		AbstractField field;
		field = new RejectionForce(REJECT_LIN);
		markerToFieldMap.put(field.getFieldMarker(), field);
		field = new AttractionForce(ATTRACT_EXP);
		markerToFieldMap.put(field.getFieldMarker(), field);
		field = new AttractionForce(ATTRACT_LIN);
		field.setDistanceFunc(new LinearFunction(10));
		field.setStrength(10);
		markerToFieldMap.put(field.getFieldMarker(), field);
		markerToFieldMap.put(
				FieldRepository.STRING, 
				new StringForce(FieldRepository.STRING));
	}

	/**
	 * @throws NullPointerException if searchResult is null.
	 */
	@Override
	public void findFields(final long fieldMarkers, final List<Field> searchResult) {
		for(long marker : markerToFieldMap.keySet()) {
			if((fieldMarkers & marker) > 0) {
				searchResult.add(markerToFieldMap.get(marker));
			}
		}
	}

	@Override
	public Field getField(final long fieldMarker) {
		return markerToFieldMap.get(fieldMarker);
	}

	@Override
	public void addField(final long fieldMarker, final Field field) {
		markerToFieldMap.put(fieldMarker, field);
	}

}
